package com.dhcc.DRGsHosp.application.impl;

import java.util.List;
import java.util.Map;

import javax.inject.Named;

import org.dayatang.querychannel.JdbcTemplateObj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Transactional;

import com.dhcc.DRGsHosp.application.MDCMonitorApplication;
import com.dhcc.DRGsHosp.core.domain.IndexCal;

/**
 * 
 * @ClassName: MDCMonitorApplicationImpl  
 * @Description ：    MDC监测
 * @Date: 2018年12月11日 上午10:27:22  
 * @Author WENYI 
 * @Version 2.5.0 DHC-DRGs
 */
@Named
@Transactional
public class MDCMonitorApplicationImpl implements MDCMonitorApplication{
	@Autowired
	private JdbcTemplateObj jdbcTemplateObj;
	@Override
	@Cacheable(value = "drgscom3.1.1", key = "'showNotCoverDRGsDetail' + #indexCal.dateFrom +#indexCal.dateTo"
			+ " +#indexCal.verRegion+#indexCal.hospCode+#indexCal.stdId+#indexCal.parmdcCode  ")
	public List<Map<String, Object>> showNotCoverDRGsDetail(IndexCal indexCal) {
		StringBuilder sql = new StringBuilder();
		String dateFrom = indexCal.getDateFrom();//开始时间
  		String dateTo = indexCal.getDateTo();//结束时间
  		String verRegion = indexCal.getVerRegion();//分组方案
  		String hospCode = indexCal.getHospCode();
  		String stdId=indexCal.getStdId();
  		String mdcCode=indexCal.getParmdcCode();
		sql.append("SELECT DRG.DRG_CODE,DRG.DRG_DESC,STD.AS_AVG_FEE,STD.AS_AVG_DAYS,STD.AS_WEIGHT,STD.AS_RISK_LEVEL FROM DRGS.T_DRGS DRG \n ");
		sql.append("LEFT JOIN DRGS.T_ADRG ADRG ON ADRG.ADRG_CODE=DRG.ADRG_CODE AND DRG.VER_CODE=ADRG.VER_CODE    \n ");
		sql.append("LEFT JOIN DRGS.T_MDCGROUP MDC ON MDC.MDC_CODE=ADRG.MDC_CODE AND MDC.VER_CODE=ADRG.VER_CODE  \n ");
		sql.append("LEFT JOIN DRGS.T_ANNUALSTD STD ON DRG.DRG_CODE=STD.AS_DRG_CODE AND STD.AS_ID='"+stdId+"' \n ");
		sql.append("WHERE DRG.VER_CODE = ( \n ");
		sql.append("	SELECT A.VER_CODE  FROM ( \n ");
		sql.append(" 		SELECT  PDG.VER_CODE,COUNT(1) CNT FROM DRGS.MR_BASEINFO A  \n ");
		sql.append(" 		LEFT JOIN DRGS.MR_IPINFO IPINFO ON A.PMAININDEX = IPINFO.PMAININDEX  \n ");
		sql.append(" 		LEFT JOIN DRGS.T_PATIENTDRG PDG ON PDG.PARID=A.PMAININDEX \n ");
		sql.append(" 		WHERE PDG.VER_CODE IS NOT NULL AND TO_CHAR(IPINFO.P00290000,'YYYY-MM-DD') BETWEEN  '"+dateFrom+"'  AND '"+dateTo+"' AND INSTR('"+hospCode+"',a.p00010001)>0 \n ");
		sql.append(" 		GROUP BY PDG.VER_CODE)  \n ");
		sql.append(" 	 A WHERE ROWNUM=1   \n ");
		sql.append(") \n ");
		sql.append("AND NOT EXISTS ( \n ");
		sql.append("	SELECT 1 FROM (SELECT DISTINCT B.PARDRG_CODE FROM  DRGS.MR_BASEINFO A  \n ");
		sql.append("	LEFT JOIN DRGS.MR_IPINFO IPINFO ON A.PMAININDEX = IPINFO.PMAININDEX   \n ");
		sql.append("	LEFT JOIN DRGS.T_PATIENTDRG B ON IPINFO.PMAININDEX=B.PARID  AND B.VER_REGION='"+verRegion+"' \n ");
		sql.append(" 	WHERE TO_CHAR(IPINFO.P00290000,'YYYY-MM-DD') BETWEEN  '"+dateFrom+"'  AND '"+dateTo+"'  \n ");
		sql.append(" 	AND B.PARDRG_CODE IS NOT NULL AND INSTR('"+hospCode+"',a.p00010001)>0 ) DISDRG WHERE DISDRG.PARDRG_CODE=DRG.DRG_CODE \n ");
		sql.append(") \n ");
		sql.append("AND MDC.MDC_CODE='"+mdcCode+"' \n ");
		List<Map<String,Object>> resultList=jdbcTemplateObj.queryForList(sql.toString());
  		return resultList;
	}

}
